#include "lobbyWidget.h"

LobbyWidget::LobbyWidget(bool server, QWidget *parent) :
    QWidget(parent) {

    playerListWgt = new QListWidget();

    b_exitBtn = new QPushButton("Leave game");
    b_startGameBtn = new QPushButton("Start Game");

    chatLine = new QLineEdit();
    chatLine->setMaxLength(100);

    QHBoxLayout* hLayout = new QHBoxLayout();
    hLayout->addWidget(new QLabel("Chat:"));
    hLayout->addWidget(chatLine);
    if (server)
        hLayout->addWidget(b_startGameBtn);
    hLayout->addWidget(b_exitBtn);

    QWidget* buttons= new QWidget();
    buttons->setLayout(hLayout);

    QVBoxLayout* vLayout = new QVBoxLayout();
    vLayout->addWidget(new QLabel("Players list:"));
    vLayout->addWidget(playerListWgt);
    vLayout->addWidget(buttons);

    setLayout(vLayout);

    connect(chatLine, SIGNAL(returnPressed()), this, SLOT(lineEditEntered()));

}

void LobbyWidget::changeList(std::vector<plInLobby> *ls) {

    qDebug() << "shanged";
    list = *ls;
    playerListWgt->clear();

    QString line;

    for (std::vector<plInLobby>::iterator it = list.begin(); it != list.end(); it++) {

        line = "";

        line += (*it).name;
        line += " [";

        line += QString::fromStdString((*it).adr.toString());
        line += "] ";

        playerListWgt->addItem(line);
    }
}

void LobbyWidget::lineEditEntered() {

    emit chatMsgOut(chatLine->text());

    qDebug() << "chat signal: " << chatLine->text();
    chatLine->setText("");
}
